<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui" xmlns:f="http://java.sun.com/jsf/core"
                xmlns:constants="http://www.tuanzi.com/constants/tags"
                template="/templates/template.xhtml">
    <ui:define name="title">短信计算任务</ui:define>
    <ui:define name="content">
        <h:form id="taskCalculateForm">
            <p:dialog header="预估用户数量中,请稍候" widgetVar="" modal="true" closable="false" resizable="false"
                      draggable="false"/>
            <p:defaultCommand target="search"/>
            <div class="topbar clearfix"
                 style="background: -webkit-linear-gradient(top,#f6f7f9 0,#ebedf0 100%);">
                <p:toolbar>
                    <f:facet name="left">
                        <p:commandButton value="批量模式"
                                         icon="fa fa-list-ul"
                                         actionListener="#{smsCalculateTaskBean.changeModeToBatch}"
                                         update="@form"
                                         process="@this"
                                         rendered="#{smsCalculateTaskBean.isNotBatch()}"/>
                        <p:commandButton value="常规模式" icon="fa fa-file"
                                         actionListener="#{smsCalculateTaskBean.changeModeToNormal}"
                                         process="@this"
                                         update="@form"
                                         rendered="#{smsCalculateTaskBean.isBatch()}"/>
                        <p:commandButton id="toggler" type="button" value="展示选择"
                                         style="width: auto;"
                                         icon="ui-icon-calculator"/>

                        <p:outputLabel for="source" value="任务名称"  style="margin-left: 20px"/>
                        <p:inputText id="source" value="#{smsCalculateTaskBean.name}"/>

                        <p:commandButton style="margin-left: 25px; float: right" value="导入"
                                         actionListener="#{smsCalculateTaskBean.onImportPhones}" icon="fa fa-upload" update="@form">
                            <p:confirm header="确认要导入吗？"/>
                        </p:commandButton>

                        <p:fileUpload style="margin-left: 25px; float: right "
                                fileUploadListener="#{smsCalculateTaskBean.handleExternalPhone}"
                                mode="advanced" auto="true"
                                sizeLimit="#{constants:get('UPLOAD_FILE_SIZE')}"
                                allowTypes="/(\.|\/)(txt)$/" label="选择文件（.txt）"
                                invalidFileMessage="文件格式不正确" />

                        <p:columnToggler datasource="smsCalculateTaskDataTable" trigger="toggler"/>

                    </f:facet>
                    <f:facet name="right">
                        <p:commandButton id="batchSendTextMessage" value="发送文本短信" icon="fa fa-search"
                                         update="@form" process="@form" action="#{smsCalculateTaskBean.onBatchSend}"
                                         rendered="#{smsCalculateTaskBean.isBatch()}"/>
                        <p:commandButton id="search" value="查询" icon="fa fa-search"
                                         update="@form" rendered="#{smsCalculateTaskBean.isNormal()}"/>
                        <p:commandButton value="重置" icon="fa fa-refresh"
                                         actionListener="#{smsCalculateTaskBean.onReset}"
                                         process="@this"
                                         update="@form" rendered="#{smsCalculateTaskBean.isNormal()}"/>
                        <p:button value="返回" icon="fa fa-reply-all" href="#{currentUserBean.back}"></p:button>
                    </f:facet>
                </p:toolbar>
            </div>
            <div class="ui-fluid" style="padding-top: 30px;">
                <div class="ui-g">
                    <div class="ui-g-12">
                        <div class="card ui-g-12">
                            <div class="ui-g-1">
                                <p:outputLabel value="名称"/>
                            </div>
                            <div class="ui-g-5">
                                <p:inputText value="#{smsCalculateTaskBean.$.name}"/>
                            </div>
                            <div class="ui-g-1">
                                <p:outputLabel value="状态"/>
                            </div>
                            <div class="ui-g-5">
                                <p:selectOneMenu
                                        value="#{smsCalculateTaskBean.$.status}" filter="true"
                                        filterMatchMode="contains">
                                    <f:selectItem itemLabel="- 请选择 -" itemValue=""
                                                  noSelectionOption="true"/>
                                    <f:selectItems
                                            value="#{smsCalculateTaskBean.dictionaries[DictionaryCode.SMS_CALCULATE_STATUS]}"
                                            var="dic"
                                            itemLabel="#{dic.name}" itemValue="#{dic.value1}"/>
                                </p:selectOneMenu>
                            </div>
                            <div class="ui-g-1">
                                <p:outputLabel value="短信数量"/>
                            </div>
                            <div class="ui-g-5">
                                <p:inputNumber value="#{smsCalculateTaskBean.$.phoneCountStart}"
                                               styleClass="autoWidthCalendar"
                                               decimalPlaces="0"/>
                                <span>—</span>
                                <p:inputNumber value="#{smsCalculateTaskBean.$.phoneCountEnd}"
                                               styleClass="autoWidthCalendar"
                                               decimalPlaces="0"/>
                            </div>
                            <div class="ui-g-1">
                                <p:outputLabel value="发送次数"/>
                            </div>
                            <div class="ui-g-5">
                                <p:inputNumber value="#{smsCalculateTaskBean.$.sentCountStart}"
                                               styleClass="autoWidthCalendar"
                                               decimalPlaces="0"/>
                                <span>—</span>
                                <p:inputNumber value="#{smsCalculateTaskBean.$.sentCountEnd}"
                                               styleClass="autoWidthCalendar"
                                               decimalPlaces="0"/>
                            </div>
                        </div>
                        <div class="card ui-g-12">
                            <p:dataTable id="smsCalculateTaskDataTable"
                                         style="width: 100%"
                                         var="smsCalculateTask"
                                         value="#{smsCalculateTaskBean.lazyDataModel}"
                                         rows="#{constants:get('ROWS')}"
                                         lazy="true"
                                         paginator="true"
                                         paginatorTemplate="#{constants:get('PAGINATOR_TEMPLATE')}"
                                         rowsPerPageTemplate="#{constants:get('ROWS_PER_PAGE_TEMPLATE')}"
                                         emptyMessage="#{constants:get('EMPTY_MESSAGE')}"
                                         scrollable="true"
                                         scrollWidth="97%"
                                         reflow="true"
                                         selection="#{smsCalculateTaskBean.selectedRecords}"
                                         resizableColumns="true">

                                <p:column selectionMode="multiple" style="width:16px;text-align:center"
                                          rendered="#{smsCalculateTaskBean.isBatch()}"/>
                                <p:column headerText="操作" width="9%" rendered="#{smsCalculateTaskBean.isNotBatch()}">
                                    <p:splitButton icon="fa fa-eye"
                                                   action="#{smsCalculateTaskBean.onViewCondition(smsCalculateTask.id)}"
                                                   process="@this">
                                        <f:setPropertyActionListener
                                                value="/views/family/user-info/send-sms/sms-calculate-task.xhtml"
                                                target="#{currentUserBean.back}"/>

                                        <p:menuitem value="发送文本短信" icon="fa fa-bullhorn"
                                                    action="#{smsCalculateTaskBean.onSend(smsCalculateTask.id)}"
                                                    process="@this" update="@form"
                                                    rendered="#{smsCalculateTask.status == 0 or smsCalculateTask.status == 1 or smsCalculateTask.status == 2}">
                                            <f:setPropertyActionListener
                                                    value="/views/family/user-info/send-sms/sms-calculate-task.xhtml"
                                                    target="#{currentUserBean.back}"/>
                                        </p:menuitem>

                                        <p:menuitem value="发送语音短信" icon="fa fa-bullhorn"
                                                    action="#{smsCalculateTaskBean.onSendVoiceSms(smsCalculateTask.id)}"
                                                    process="@this" update="@form"
                                                    rendered="#{smsCalculateTask.status == 0 or smsCalculateTask.status == 1 or smsCalculateTask.status == 2}">
                                            <f:setPropertyActionListener
                                                    value="/views/family/user-info/send-sms/sms-calculate-task.xhtml"
                                                    target="#{currentUserBean.back}"/>
                                        </p:menuitem>

                                        <p:menuitem value="取消" icon="fa fa-remove"
                                                    action="#{smsCalculateTaskBean.onCancel(smsCalculateTask.id)}"
                                                    process="@this" update="@form"
                                                    rendered="#{smsCalculateTask.status == 0}">
                                            <p:confirm header="提示信息" message="确定取消?" icon="ui-icon-alert"/>
                                        </p:menuitem>

                                        <p:menuitem value="查看错误信息" icon="fa fa-warning"
                                                    actionListener="#{smsCalculateTaskBean.onViewError(smsCalculateTask.id)}"
                                                    oncomplete="PF('errorReasonDialogWidgeVar').show();"
                                                    update=":taskCalculateForm:errorReasonDialog"
                                                    rendered="#{smsCalculateTask.status == 3}"/>

                                        <p:menuitem value="再次计算" icon="fa fa-repeat"
                                                    action="#{smsCalculateTaskBean.onRecalculate(smsCalculateTask.id)}"
                                                    process="@this" update="@form"
                                                    rendered="#{smsCalculateTask.status == 4 or smsCalculateTask.status == 3}"/>

                                        <p:menuitem value="唤醒" icon="fa fa-check-circle"
                                                    action="#{smsCalculateTaskBean.onActive(smsCalculateTask.id)}"
                                                    process="@this" update="@form"
                                                    rendered="#{smsCalculateTask.status == 5}"/>
                                    </p:splitButton>
                                </p:column>
                                <p:column headerText="计算任务ID">
                                    <h:outputText value="#{smsCalculateTask.id}"/>
                                </p:column>
                                <p:column headerText="名称">
                                    <h:outputText value="#{smsCalculateTask.name}"/>
                                </p:column>
                                <p:column headerText="状态">
                                    <h:outputText
                                            value="#{smsCalculateTaskBean.translate(DictionaryCode.SMS_CALCULATE_STATUS,smsCalculateTask.status)}"/>
                                </p:column>
                                <p:column headerText="短信数量">
                                    <h:outputText value="#{smsCalculateTask.phoneCount}"/>
                                </p:column>
                                <p:column headerText="发送次数">
                                    <h:outputText value="#{smsCalculateTask.sentCount}"/>
                                </p:column>
                                <p:column headerText="提交时间">
                                    <h:outputText value="#{smsCalculateTask.createTime}">
                                        <f:convertDateTime pattern="#{constants:get('DATETIME_FORMAT')}"/>
                                    </h:outputText>
                                </p:column>
                                <p:column headerText="计算开始时间">
                                    <h:outputText value="#{smsCalculateTask.calculateTime}">
                                        <f:convertDateTime pattern="#{constants:get('DATETIME_FORMAT')}"/>
                                    </h:outputText>
                                </p:column>
                                <p:column headerText="完成时间">
                                    <h:outputText value="#{smsCalculateTask.finishTime}">
                                        <f:convertDateTime pattern="#{constants:get('DATETIME_FORMAT')}"/>
                                    </h:outputText>
                                </p:column>
                            </p:dataTable>
                        </div>
                    </div>
                </div>
            </div>

            <p:dialog id="errorReasonDialog" header="任务异常信息" widgetVar="errorReasonDialogWidgeVar"
                      modal="true"
                      resizable="false"
                      closable="true"
                      width="1024"
                      height="500" responsive="true">
                <div class="ui-g ui-fluid">
                    <div class="ui-g-12">
                        <p:inputTextarea rows="20"
                                         value="#{smsCalculateTaskBean.taskErrorReason}" readonly="true"/>
                    </div>
                </div>
            </p:dialog>
        </h:form>
    </ui:define>
</ui:composition>
